package com.dtruth.hadoop.hdfs2hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HBaseUtil {

	/**
	 * create a table
	 * 
	 * @param tablename
	 * @throws ZooKeeperConnectionException
	 * @throws MasterNotRunningException
	 * @throws IOException
	 */
	public static void createHBaseTable(String tablename)
			throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		TableName tn = TableName.valueOf(tablename);
		HTableDescriptor htd = new HTableDescriptor(tn);
		HColumnDescriptor col = new HColumnDescriptor("content");
		htd.addFamily(col);
		Configuration cfg = HBaseConfiguration.create();
		try (HBaseAdmin admin = new HBaseAdmin(cfg)) {
			if (admin.tableExists(tablename)) {
				System.out.println("table exists,trying recreate table!");
				admin.disableTable(tablename);
				admin.deleteTable(tablename);
				admin.createTable(htd);
			} else {
				System.out.println("create new table:" + tablename);
				admin.createTable(htd);
			}

		}
	}
}
